﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Cartoon.Logic;
using Belyn.Common.Web;
using Cartoon.Model;
using Cartoon.Common;

namespace CartoonViewer
{
    /// <summary>
    /// UC_DownloadPanel.xaml 的交互逻辑
    /// </summary>
    public partial class UC_DownloadPanel : UserControl
    {
        public UC_DownloadPanel()
        {
            InitializeComponent(); 
           
        }

        protected void SetDownloadingCount(int count)
        {
            this.TxtDownloading.Dispatcher.Invoke(new Action(() => {
                this.TxtDownloading.Text = "正在下载 (" + count + ")";
            }));
           
        }
        protected void SetDownloadedCount(int count)
        {
            this.TxtDownloading.Dispatcher.Invoke(new Action(() =>
            {
                this.TxtDownloading.Text = "已下载 (" + count + ")";
            }));
            
        }
        protected System.Collections.Generic.Dictionary<string, UC_DownloadItem> DownloadingList = new Dictionary<string, UC_DownloadItem>();
        protected void LoadDownloadingList()
        {
            this.SetDownloadingCount(this.DownloadingList.Count);
            foreach (IDownload d in this.DownloadingList.Values)
            {
                
                this.AddDownloading(d);
            }
        }

        protected System.Collections.Generic.Dictionary<string, IDownload> DownloadedList = new Dictionary<string, IDownload>();
        protected void LoadDownloadedList()
        {

        }

        private CartoonDownloadManage _DownloadManage;
        public CartoonDownloadManage DownloadManage
        {
            get 
            { 
                return this._DownloadManage; 
            }
            set 
            {
                this._DownloadManage = value;

                this.DownloadingList.Clear();
                foreach (IDownload d in this._DownloadManage.DownloadList)
                {
                    UC_DownloadItem item = new UC_DownloadItem(d);
                    this.DownloadingList.Add(d.Title, item);
                }

                this._DownloadManage.Added += new Belyn.Common.Web.DownloadDelegate(_DownloadManage_Added);
                //this._DownloadManage.Downloading += new Belyn.Common.Web.DownloadDelegate(_DownloadManage_Downloading);
                this._DownloadManage.ListChanged += new EventHandler(_DownloadManage_ListChanged);
                this._DownloadManage.Removed += new Belyn.Common.Web.DownloadDelegate(_DownloadManage_Removed);
                //this._DownloadManage.SpeedChanged += new DownloadDelegate(_DownloadManage_SpeedChanged);
                //this._DownloadManage.Failed += new DownloadFailedDelegate(_DownloadManage_Failed);
                this.LoadDownloadingList();
            }
        }

        void _DownloadManage_Failed(IDownload d, string message)
        { 
            this._DownloadManage.Remove(d);
            this.RemoveDownloading(d);
        }  

        void _DownloadManage_SpeedChanged(IDownload d)
        { 
            this.UpdateDownloading(d); 
        }

        protected void UpdateDownloading(IDownload d) 
        {
            
        } 


        void _DownloadManage_Removed(Belyn.Common.Web.IDownload d)
        {
            this.RemoveDownloading(d); 
        } 

        protected void RemoveDownloading(Belyn.Common.Web.IDownload d)
        {
            if (this.DownloadingList.Keys.Contains(d.Title) == false)
            {
                return; 
            }
            this.DownloadingList.Remove(d.Title);
            this.LvDownloading.RemoveItem(d.Title);
            this.SetDownloadingCount(this.DownloadingList.Count);
        }

        void _DownloadManage_ListChanged(object sender, EventArgs e)
        {
             
        }

        void _DownloadManage_Downloading(Belyn.Common.Web.IDownload d)
        {
            if (d.Over == true)
            {
                Log.log(d.Title + " 下载完成...");
            }
        }

        void _DownloadManage_Added(Belyn.Common.Web.IDownload d)
        { 
            this.AddDownloading(d);
            //d.SpeedChanged += new DownloadDelegate(_DownloadManage_SpeedChanged);
            //d.Downloading += new Belyn.Common.Web.DownloadDelegate(_DownloadManage_Downloading);
            //d.Failed += new DownloadFailedDelegate(_DownloadManage_Failed);
        } 
        protected void AddDownloading(Belyn.Common.Web.IDownload d)
        {  
            if (this.DownloadingList.Keys.Contains(d.Title) == true)
            {
                return; 
            }

            UC_DownloadItem newItem = new UC_DownloadItem(d);
            this.LvDownloading.AddItem(newItem);
            this.DownloadingList.Add(newItem.Title, newItem);

            this.SetDownloadingCount(this.DownloadingList.Count);

        }
        private void TxtDownloaded_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.HideDownloading(); 
        }

        private void TxtDownloading_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        { 
            this.ShowDownloading();
        }

        protected void ShowDownloading()
        {
            this.LvDownloading.Visibility = Visibility.Visible;
        }   
        protected void HideDownloading()
        {
            this.LvDownloading.Visibility = Visibility.Collapsed;
        }
 
        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            foreach (UC_DownloadItem downloadItem in this.LvDownloading.SelectedItems)
            { 
                downloadItem.Start();
            }
        }

        private void BtnSuspend_Click(object sender, RoutedEventArgs e)
        {
            foreach (UC_DownloadItem downloadItem in this.LvDownloading.SelectedItems)
            {
                downloadItem.Suspend();
            }
        }

        private void BtnStop_Click(object sender, RoutedEventArgs e)
        {
            var ILDownLoadItemForCancel = this.LvDownloading.SelectedItems.ToList();
            foreach (var downloadItem in ILDownLoadItemForCancel)
            {
                downloadItem.Cancel();
            }
        }
    }
}
